#include "CollisionManager.h"

void CollisionManager::checkPlayerTileCollision(Player * pPlayer,
        const std::vector<TileLayer*> &collisionLayers)
{
    for (std::vector<TilePlayer*>::const_iterator it =
            collisionLayers.begin();
            it != collisionLayers.end();
            ++it){
        TileLayer * pTileLayer = (*it);
        std::vector<std::vector<int>> tiles = pTileLayer->getTileIDs();

        Vector2D layerPos = pTileLayer->getPosition();

        int x, y;
        int tileColumn, tileRow, tileid = 0;
    
        x = layerPos.getX() / pTileLayer->getTileSize();
        y = layerPos.getY() / pTileLayer->getTileSize();

        if (pPlayer->getVelocity()->getX() >= 0
        ||  pPlayer->getVelocity()->getY() >= 0)
        {
            tileColunm = ((pPlayer->getPosition().getX() 
                    + pPlayer->getWidth()) / pTileLayer->getTileSize());
            tileRow = ((pPlayer->getPosition().getY() 
                    + pPlayer->getHeighht())/ pTileLayer->getTileSize());
            tileid = tiles[tileRow + y][tileColumn + x];
        }
        else if( pPlayer->getPosition().getX() < 0
        ||       pPlayer->getPosition().getY() < 0)
        {
           tileColumn = pPlayer->getPosition().getX() / pTileLayer->getTileSize();           tileRow = pPlayer->getPosition().getY() / pTileLayer->getTileSize();  
           tileid = tiles[tileRow + y][tileColumn + x];
        }
        if (tileid != 0)
        {
            pPlayer->collision();
        }

    }






}
